﻿using EntityModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace EGS.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AccountController : ControllerBase
    {
        private readonly EGSDbContext db;
        public AccountController(EGSDbContext context)
        {
            db = context;
        }

        [HttpGet("GetAll")]
        public List<EGS_Account> GetAll()
        {
            return db.Account.ToList();
        }



        [HttpGet("{id}")]
        public EGS_Account? GetEGS_AccountById(Guid id)
        {
            EGS_Account? account = new EGS_Account();
            account = db.Account.Where(a => a.ID == id).FirstOrDefault();
            return account;
        }
        [HttpGet("like/{name}")]
        public List<EGS_Account> GetEGS_AccountsLikeName(string name)
        {
            List<EGS_Account> account = new List<EGS_Account>();
            account = db.Account.Where(a => a.Account.Contains(name)).ToList();
            return account;
        }

        [HttpPost]
        public Result AddAccount([FromBody] EGS_Account account)
        {
            Result result = new Result();
            db.Account.Add(account);
            result.Data = db.SaveChanges();
            return result;
        }
        [HttpDelete]
        public bool Delete(Guid ID)
        {
            EGS_Account? account = new EGS_Account();
            account = db.Account.Where(a => a.ID == ID).FirstOrDefault();

            int result = 0;
            if (account != null)
            {
                account.DelTime = DateTime.Now;
                db.Account.Update(account);
                result = db.SaveChanges();
            }
            return result > 0 ? true : false;
        }
        [HttpPut]
        public bool Update(EGS_Account account)
        {
            db.Account.Update(account);
            int result = db.SaveChanges();
            return result > 0 ? true : false;
        }
        //[HttpPut]
        //public Result UpAccount([FromBody] EGS_Account account)
        //{
        //    Result result = new Result();
        //    db.Account.Update(account);
        //    result.Data = db.SaveChanges();

        //    return result;
        //}
        //[HttpGet]
        //public Result Get(Guid id)
        //{
        //    Result result = new Result();
        //    result.Data = db.Account.Where(a => a.ID == id).FirstOrDefault();

        //    return result;
        //}
        
        
    }
}
